
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js'); 
var actmodel={
  qqmapsdk:null,
  getqqmapsdk(){
    if(!this.qqmapsdk)
    {
      var app = getApp();
      var map = app.globalData.configdata.map
      actmodel.qqmapsdk = new QQMapWX({
        key: map["qq"].key// 必填     
      });
    }
   return actmodel.qqmapsdk;
  },  
  search(keyword){
 
    return new Promise(resolve => {

      if(!keyword)
      {
        actmodel.locationaddr().then(resdata=>{
          if(resdata["status"]==1){
            resolve({status:1,data:resdata.pois })
          }
          else
          {
            resolve(resdata)
          }

        })
      }
      else
      {
        actmodel.getqqmapsdk().search({
          keyword: keyword,  //搜索关键词
          success: function (res) { //搜索成功后的回调         
  
            resolve({status:1,data:res.data })           
      
          },
          fail: function (res) {
            resolve({status:-1,message:"搜索失败" })
          },
          complete: function (res) {
           
          }
        } )
      }
     
    })
    
  },
  
/**
 * 
 * @param {*} location   //{lat,lng} 
 * @param {*} rest //暂时没用
 * @param {*} get_poi   //是否获取相关地址  0不获取， 1获取
 */
  locationaddr:function(location,rest,get_poi){
    if(get_poi!==0){
      get_poi =1
    }
    return new Promise(resolve => {
      var config={ 
        get_poi:get_poi,     
        success: function (res) { 
            
         var addrdata =res.result
         var position=addrdata.location
      

          var address_component=addrdata.address_component;
          var locationaddr={lat:position.lat,lng:position.lng}
          locationaddr["status"]=1
          var fields=["province","city","district","street"]
          if(address_component)
          {
            fields.forEach(item=>{
              if(address_component[item])
              {
                locationaddr[item]=address_component[item]
              }
              else
              {
                locationaddr[item]="" 
              }             
            })        
          }
          if( addrdata.formatted_addresses){
            locationaddr["addrinfo"]=addrdata.formatted_addresses["recommend"];
            locationaddr["addrname"]=addrdata["address"];
          }
          if(get_poi)
          {
            locationaddr["pois"]=addrdata["pois"]  
          }
          locationaddr["adcode"] = addrdata["ad_info"]["adcode"]
          locationaddr["raw"] ={address:addrdata["address"],ad_info:addrdata["ad_info"],location:addrdata["location"],formatted_addresses:addrdata["formatted_addresses"]}         
          resolve(locationaddr)          
          //搜索成功后的回调
        },
        fail: function (res) {
     
          resolve({status:-1,'message':'获取城市失败'})      
        },
        complete: function (res) { 
        }
        
      };


      if(location)
      {
        config["location"]=location["lat"] +","+location["lng"]
      }

      
      actmodel.getqqmapsdk().reverseGeocoder(config )
    
    })
  }

}

//  this.data.qqmapsdk.reverseGeocoder(config );
 
module.exports = actmodel